(function(app){
	// 好友列表
	app.action(['/pay/frindList'], 'pay/page.html', ['$scope', '$route', '_API', '$location', 	'$timeout', '$rootScope',
		function($scope, $route, _API, $location, $timeout, $rootScope) {
			function reload(){
				_API.ajax('person/contact/list',{})
				.success(function(json){
					$scope.data = json.result;
				})
				.error(function(json){
					if(json.code == '201'){
						alert('请登陆手机端，完成实名认证！');
						$location.path("/certification");
					}
				});
			}
			reload();
			//历年补缴
			$("#button1").click(function(){
				var $subBox = $("input[name='radioBox']");
				var $MINYEAR = $("input[name='MINYEAR']");
				var $MAXYEAR = $("input[name='MAXYEAR']");
				var ids = "";
				var MINYEAR = "";
				var MAXYEAR = "";
				for (var i=0; i<$subBox.length; i++) {
					var box = $subBox[i];
					var min = $MINYEAR[i];
					var max = $MAXYEAR[i];
					if(box.checked){
						ids = box.value;
						MINYEAR = min.value;
						MAXYEAR = max.value;
					}
				}
				if(ids == "") {
					alert("请选择要缴费的用户。");
					return;
				} else {
					// 判断该好友是否有已存在订单，如果存在则跳转至订单页面，否则跳转至年度选择页面
					_API.ajax('order/unDealOrder',{"CA001":ids})
					.success(function(json){
						if(json.result.isPass) {
							// 订单存在
							alert("您有未支付订单，请先处理！");
							$location.path("/pay/order/"+ids);
						} else {
							// 订单不存在
							$location.path("/pay/nextYear/"+ids+","+MINYEAR+","+MAXYEAR);
						}
					});
				}
			});
			
			//正常缴费
			$scope.save2 = function() {
				var $subBox = $("input[name='radioBox']");
				var $MINYEAR = $("input[name='MINYEAR']");
				var $MAXYEAR = $("input[name='MAXYEAR']");
				var ids = "";
				var MAXYEAR = "";
				for (var i=0; i<$subBox.length; i++) {
					var box = $subBox[i];
					var max = $MAXYEAR[i];
					if(box.checked){
						ids = box.value;
						MAXYEAR = max.value;
					}
				}
				if(ids == "") {
					alert("请选择要缴费的用户。");
					return;
				} else {
					// 判断该好友是否有已存在订单，如果存在则跳转至订单页面，否则跳转至年度选择页面
					_API.ajax('order/unDealOrder',{"CA001":ids})
					.success(function(json){
						if(json.result.isPass) {
							// 订单存在
							alert("您有未支付订单，请先处理！");
							$location.path("/pay/order/"+ids);
						} else {
							// 订单不存在
							var str = ids + "," +MAXYEAR+","+MAXYEAR;
							$location.path("/pay/next/"+str);
						}
					});
				}
			}
		}
	]);
	// 跳转至已存在订单页面
	app.action(['/pay/order/:ids'], 'pay/order.html', 
			['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
   		function($scope, $route, _API, $location, $timeout, $rootScope) {
			function load() {
				_API.ajax('order/unDealOrder',{"CA001":$route.current.params.ids})
				.success(function(json){
					if(json.result.isPass) {
						// 订单存在
						$scope.d = json.result.datas;
					}
				});
			}
			load();
			
			// 作废订单
			$scope.remove = function(data) {
				_API.ajax('order/cancleOrder',{"AAZ288":data.AAZ288})
				.success(function(json){
					if(!json.result.isPass) {
						alert(json.result.info);
					} else {
						alert("作废成功。");
						$location.path("/pay/frindList");
					}
				});
			}
			// 支付订单
			$scope.pay = function(data) {
				sessionStorage.setItem("AAZ288",data.AAZ288);
				sessionStorage.setItem("BAB502",data.BAB502);
				var shade = document.getElementById('shade'); //支付时页面形成遮罩
				shade.style.display = 'block';
				var orderNumber = data.AAZ288; // 订单号
				var orderAmount = data.AAE022; // 订单金额
				var bankType = data.BAB502; // 银行类别
				window.open($SDC.apiDomain+"/payment?orderNumber="+orderNumber+"&orderAmount="+orderAmount+"&BAB502="+bankType
				+"&payType=1");
			}

			$scope.payOK = function() {
				var shade = document.getElementById('shade'); //支付时页面形成遮罩
				shade.style.display = 'none';
				var AAZ288 = sessionStorage.getItem("AAZ288");
				var BAB502 = sessionStorage.getItem("BAB502");
				
				_API.ajax('order/checkOrder',{"AAZ288":AAZ288,"BAB502":BAB502})
				.success(function(json){
					if(json.result.isPass === true){
						reload();
					}
				});
			}

   		}
   	]);
	// 缴费年度选择页面
	app.action(['/pay/nextYear/:ids'], 'pay/editYear.html', 
			['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
   		function($scope, $route, _API, $location, $timeout, $rootScope) {
			// 页面初始化数据
			var str = $route.current.params.ids;
			var info = str.split(",");
			var CA001 = info[0];
			var MINYEAR = info[1];
			var MAXYEAR = info[2];
			$scope.minYear = new Array();
			$scope.maxYear = new Array();
			for (var i=MINYEAR; i<MAXYEAR; i++) {
				$scope.minYear.push(i);	
			}
			for (var i=MAXYEAR-1; i>=MINYEAR; i--) {
				$scope.maxYear.push(i);	
			}

			_API.ajax('homePage/myTransfer/getInfoByCa001',{"CA001":CA001})
			.success(function(json){
				$scope.data3 = json.result;
			});
			// 跳转至缴费记录页面
			$scope.next = function(data, data3) {
				if(!$("#form").valid()) {
					return false;
				}
				
				var str = data3.CA001+","+$("#startYear").val()+","+$("#endYear").val();
				$location.path("/pay/next/"+str);
			}
   		}
   	]);
	// 缴费编辑页面
	app.action(['/pay/next/:str'], 'pay/edit.html', 
			['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
   		function($scope, $route, _API, $location, $timeout, $rootScope) {
			var str = $route.current.params.str;
			var array = new Array();
			array = str.split(",");
			// 页面初始化数据
			_API.ajax('homePage/myTransfer/getInfoByCa001',{"CA001":array[0]})
			.success(function(json){
				$scope.data3 = json.result;
			});
			function reload(){
				// 缴费记录
				_API.ajax('homePage/myPay/getPayRecords',{"CA001":array[0],"startYear":
					array[1],"endYear":array[2]})
				.success(function(json){
					var jsonStr = angular.fromJson(json);
					if(jsonStr.result.isPass == false) {
						alert(jsonStr.result.info);
						$location.path("/pay/frindList");
					} else {
						$scope.data = jsonStr.result.datas;
					}
				})
			}
			reload();
			$scope.nextOrder = function(data,data3) {
				var $subBox = $("input[name='checkBox']");
				var ids = "";
				for (var i=0; i<$subBox.length; i++) {
					var box = $subBox[i];
					if(box.checked){
						ids += box.value+"$";
					}
				}
				if(ids == "") {
					alert("请选择缴费记录。");
					return;
				} else {
					var array = new Array();
					array = ids.split("$");
					var str = "[";
					var flag = "";
					for(var i = 0;i < array.length; i++) {
						if(array[i] != null && array[i] != "" && array[i] != undefined) {
							var id = array[i].split(",");
							var year = id[0]; // 年度
							var aaz289 = id[1]; // 缴费档次
							if(aaz289 == "" || aaz289 == undefined) {
								flag = "error";
								alert(year+"年度请输入缴费档次！");
								break;
							} else {
								str += "{'AAE003':"+year+",'AAZ289':"+aaz289+"},";
							}
						}
					}
					if(flag != "error") {
						str = str.substring(0, str.length-1)+"]";
						$location.path("/pay/nextOrder/"+str+"$"+data3.CA001);
					}
				}
				
			}
			
			
   		}
   	]);
	// 订单支付页面
	app.action(['/pay/nextOrder/:str'], 'pay/editOrder.html', 
			['$scope', '$route', '_API', '$location', '$timeout', '$rootScope',
   		function($scope, $route, _API, $location, $timeout, $rootScope) {
			var str = $route.current.params.str;
			var array = new Array();
			array = str.split("$");
			function reload(){
				// 生成订单
				_API.ajax('homePage/myPay/generateOrder',{"CA001":array[1],"records":array[0]})
				.success(function(json){
					if(!json.result.isPass) {
						alert(json.result.info);
						$location.path("/pay/frindList");
						return;
					} else {
						_API.ajax('order/unDealOrder',{"CA001":array[1]})
						.success(function(json){
							$scope.d = json.result.datas;
						});
					}
				});
			}
			reload();
			
			$scope.remove = function(d) {
				_API.ajax('order/cancleOrder',{"AAZ288":d.AAZ288})
				.success(function(json){
					if(json.result.isPass === true){
						alert(json.result.info);
						$location.path("/pay/frindList");
					} else {
						alert(json.result.info);
					}
				});
			}
			
			$scope.pay = function(d) {
				sessionStorage.setItem("AAZ288",d.AAZ288);
				sessionStorage.setItem("BAB502",d.BAB502);
				var shade = document.getElementById('shade'); //支付时页面形成遮罩
				shade.style.display = 'block';
				var orderNumber = d.AAZ288; // 订单号
				var orderAmount = d.AAE022; // 订单金额
				var bankType = d.BAB502; // 银行类别
				window.open($SDC.apiDomain+"/payment?orderNumber="+orderNumber+"&orderAmount="+orderAmount+"&BAB502="+bankType);
			}

			$scope.payOK = function() {
				var shade = document.getElementById('shade'); //支付时页面形成遮罩
				shade.style.display = 'none';
				var AAZ288 = sessionStorage.getItem("AAZ288");
				var BAB502 = sessionStorage.getItem("BAB502");

				_API.ajax('order/checkOrder',{"AAZ288":AAZ288,"BAB502":BAB502})
				.success(function(json){
					if(json.isPass === true){
						$location.path("/orderList");
					}
				});
			}

   		}
   	]);
	
})($SDC.app);
